# -*- coding: utf-8 -*-
# Created by 'Zhou Bingbing'  on 2019/4/18
import hashlib
import random
import time
from config_setting.config_setting import session


class YouDao():
    def get_langue(self,text,FROM=None,TO=None):
        url='http://fanyi.youdao.com/bbk/translate_m.do'
        # url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
        u = 'fanyideskweb'
        d = text
        f = str(int(time.time() * 1000) + random.randint(1, 10))
        c = 'n%A-rKaT5fb[Gy?;N5@Tj'
        sign = hashlib.md5((u + d + f + c).encode('utf-8')).hexdigest()
        headers={
            'Accept':'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding':'gzip, deflate',
            'Accept-Language':'zh-CN,zh;q=0.8',
            'Connection':'keep-alive',
            'Content-Length':'221',
            'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie':'OUTFOX_SEARCH_USER_ID=43732522@10.169.0.84; OUTFOX_SEARCH_USER_ID_NCOO=1140330123.9603255; _ntes_nnid=3b993b2e5140076960c407074f2ceb2e,1555556922327; P_INFO=m13270870157@163.com|1557192709|0|other|00&99|null&null&null#shh&null#10#0#0|132157&1|mail163|13270870157@163.com; JSESSIONID=abcNOtU60Z9qUbgYBDdWw; SESSION_FROM_COOKIE=unknown; ___rl__test__cookies='+f,
            'Host':'fanyi.youdao.com',
            'Origin':'http://fanyi.youdao.com',
            'Referer':'http://fanyi.youdao.com/',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0',
            'X-Requested-With':'XMLHttpRequest',
        }
        if 'en'==FROM or 'en'==TO:
            data={
                'i':d,
                'client':'fanyideskweb',
                'salt':f,
                'sign':sign,
                'from':FROM,
                'to':TO,
                'doctype':'json',
                'version':'3.0',
                'cache':'true',
            }
        else:
            data={
                'i': d,
                'from':FROM,
            'to':TO,
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'salt':f,
            'sign':sign,
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_REALTlME',
            }
        re=session.post(url,data=data,headers=headers)
        results=re.json()
        results=results['translateResult']
        result=[]
        for i in results:
            result.append(i['tgt'].strip('\r\n'))
        return result
    @staticmethod
    def trans2other1(text,FROM='ch',TO=None):
        url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
        u = 'fanyideskweb'
        d = text
        f = str(int(time.time() * 1000) + random.randint(1, 10))
        c = 'n%A-rKaT5fb[Gy?;N5@Tj'
        sign = hashlib.md5((u + d + f + c).encode('utf-8')).hexdigest()
        headers = {
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.8',
            'Connection': 'keep-alive',
            'Content-Length': '221',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie': 'OUTFOX_SEARCH_USER_ID=43732522@10.169.0.84; OUTFOX_SEARCH_USER_ID_NCOO=1140330123.9603255; _ntes_nnid=3b993b2e5140076960c407074f2ceb2e,1555556922327; P_INFO=m13270870157@163.com|1557192709|0|other|00&99|null&null&null#shh&null#10#0#0|132157&1|mail163|13270870157@163.com; JSESSIONID=abcNOtU60Z9qUbgYBDdWw; SESSION_FROM_COOKIE=unknown; ___rl__test__cookies='+f,
            'Host': 'fanyi.youdao.com',
            'Origin': 'http://fanyi.youdao.com',
            'Referer': 'http://fanyi.youdao.com/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0',
            'X-Requested-With': 'XMLHttpRequest',
        }
        data = {
            'i': d,
            'from': FROM,
            'to': TO,
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'salt': f,
            'sign': sign,
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_REALTlME',
        }
        re = session.post(url, data=data, headers=headers)
        results = re.json()
        results = results['translateResult'][0][0]['tgt']

        return results

    @staticmethod
    def trans2other2(text, FROM=None, TO='ch'):
        url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
        u = 'fanyideskweb'
        d = text
        f = str(int(time.time() * 1000) + random.randint(1, 10))
        c = 'n%A-rKaT5fb[Gy?;N5@Tj'
        sign = hashlib.md5((u + d + f + c).encode('utf-8')).hexdigest()
        headers = {
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.8',
            'Connection': 'keep-alive',
            'Content-Length': '221',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie': 'OUTFOX_SEARCH_USER_ID=43732522@10.169.0.84; OUTFOX_SEARCH_USER_ID_NCOO=1140330123.9603255; _ntes_nnid=3b993b2e5140076960c407074f2ceb2e,1555556922327; P_INFO=m13270870157@163.com|1557192709|0|other|00&99|null&null&null#shh&null#10#0#0|132157&1|mail163|13270870157@163.com; JSESSIONID=abcNOtU60Z9qUbgYBDdWw; SESSION_FROM_COOKIE=unknown; ___rl__test__cookies=' + f,
            'Host': 'fanyi.youdao.com',
            'Origin': 'http://fanyi.youdao.com',
            'Referer': 'http://fanyi.youdao.com/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0',
            'X-Requested-With': 'XMLHttpRequest',
        }
        data = {
            'i': d,
            'from': FROM,
            'to': TO,
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'salt': f,
            'sign': sign,
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_REALTlME',
        }
        re = session.post(url, data=data, headers=headers)
        results = re.json()
        results = results['translateResult'][0][0]['tgt']
        return results
    @staticmethod
    def main(text):
        rest=[]
        results= YouDao().get_langue(text, FROM='zh-CHS', TO='en')
        for i in results:
            rest.extend(YouDao().get_langue(i, FROM='en', TO='zh-CHS'))
        for its in ['ja','ko','fr','de','ru','es','pt','it','vi','id','ar']:
            result = YouDao.trans2other1(text, TO=its)
            res = YouDao.trans2other2(result, FROM=its)
            rest.append(res)
        return rest

# if __name__=="__main__":
#     while 1:
#         text=input('请输入中文语料：')
#         if not text:
#             break
#         res=YouDao.main(text)
#         print(res)

